<template>
  <div class="login-container">
    <div class="login-background">
      <div class="background-decoration">
        <div class="decoration-circle circle-1"></div>
        <div class="decoration-circle circle-2"></div>
        <div class="decoration-circle circle-3"></div>
        <div class="decoration-line line-1"></div>
        <div class="decoration-line line-2"></div>
      </div>
    </div>
    
    <div class="login-content">
      <div class="login-card">
        <div class="login-header">
          <div class="logo-section">
            <div class="logo-icon">
              <el-icon size="32"><Shop /></el-icon>
            </div>
            <h2 class="login-title">欢迎回来</h2>
            <p class="login-subtitle">登录您的二手交易账户</p>
          </div>
        </div>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username" class="form-item">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="large"
              prefix-icon="User"
              clearable
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="password" class="form-item">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              prefix-icon="Lock"
              show-password
              clearable
              @keyup.enter="handleLogin"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item class="form-item">
            <el-button
              type="primary"
              size="large"
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              <span v-if="!loading">立即登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <p class="register-text">
            还没有账号？
            <router-link to="/register" class="register-link">
              <span>立即注册</span>
              <el-icon><ArrowRight /></el-icon>
            </router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { USER_ROLES, USER_ROLE_NAMES } from '@/types'
import { getDefaultRedirectPath, getUserRoleName } from '@/utils/auth'
import { Shop, ArrowRight } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 登录方法
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const result = await userStore.login(loginForm)
    ElMessage.success('登录成功')
    
    // 根据用户角色跳转到不同页面
    const userRole = result.userRole
    const roleName = getUserRoleName(userRole)
    
    // 获取重定向地址（如果有的话）
    const redirect = router.currentRoute.value.query.redirect as string
    
    if (userRole === USER_ROLES.ADMIN) {
      // 管理员跳转到后台管理系统
      ElMessage.info(`欢迎${roleName}，正在跳转到后台管理系统...`)
      router.push('/admin')
    } else if (userRole === USER_ROLES.USER) {
      // 普通用户跳转逻辑
      if (redirect) {
        // 如果有重定向地址，优先跳转到重定向地址
        router.push(redirect)
      } else {
        // 默认跳转到用户中心
        ElMessage.info(`欢迎回来，正在跳转到首页...`)
        router.push('/')
      }
    } else {
      // 未知角色，跳转到首页
      ElMessage.warning('用户角色未知，跳转到首页')
      router.push('/')
    }
  } catch (error: any) {
    if (error.message) {
      ElMessage.error(error.message)
    } else {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.background-decoration {
  position: relative;
  width: 100%;
  height: 100%;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 8s ease-in-out infinite;
}

.circle-1 {
  width: 120px;
  height: 120px;
  top: 10%;
  right: 15%;
  animation-delay: 0s;
}

.circle-2 {
  width: 80px;
  height: 80px;
  bottom: 20%;
  left: 10%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 60%;
  right: 25%;
  animation-delay: 4s;
}

.decoration-line {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
}

.line-1 {
  width: 200px;
  height: 2px;
  top: 30%;
  left: 5%;
  transform: rotate(45deg);
  animation: slideIn 6s ease-in-out infinite;
}

.line-2 {
  width: 150px;
  height: 2px;
  bottom: 40%;
  right: 10%;
  transform: rotate(-30deg);
  animation: slideIn 6s ease-in-out infinite 3s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-30px) rotate(5deg); }
}

@keyframes slideIn {
  0%, 100% { opacity: 0.3; transform: translateX(-50px) rotate(45deg); }
  50% { opacity: 0.8; transform: translateX(0px) rotate(45deg); }
}

.login-content {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 450px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  padding: 50px 40px;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #ff6b35 0%, #f7931e 100%);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.logo-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 8px 25px rgba(255, 107, 53, 0.3);
  animation: bounceIn 1s ease-out;
}

.login-title {
  font-size: 32px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
  letter-spacing: -0.5px;
  animation: fadeInUp 1s ease-out 0.2s both;
}

.login-subtitle {
  font-size: 16px;
  color: #7f8c8d;
  margin: 0;
  font-weight: 400;
  animation: fadeInUp 1s ease-out 0.4s both;
}

.login-form {
  margin-bottom: 30px;
}

.form-item {
  margin-bottom: 24px;
}

.custom-input {
  --el-input-border-radius: 12px;
  --el-input-bg-color: #f8f9fa;
  --el-input-border-color: #e1e8ed;
  --el-input-hover-border-color: #ff6b35;
  --el-input-focus-border-color: #ff6b35;
  --el-input-focus-bg-color: #fff;
}

.custom-input :deep(.el-input__wrapper) {
  box-shadow: none;
  border: 2px solid var(--el-input-border-color);
  transition: all 0.3s ease;
  padding: 12px 16px;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: var(--el-input-hover-border-color);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(255, 107, 53, 0.1);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: var(--el-input-focus-border-color);
  background-color: var(--el-input-focus-bg-color);
  box-shadow: 0 8px 25px rgba(255, 107, 53, 0.15);
}

.custom-input :deep(.el-input__inner) {
  font-size: 16px;
  font-weight: 500;
  color: #2c3e50;
}

.custom-input :deep(.el-input__prefix-inner) {
  color: #7f8c8d;
}

.login-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px;
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  border: none;
  color: white;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.login-btn:hover::before {
  left: 100%;
}

.login-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 30px rgba(255, 107, 53, 0.3);
}

.login-btn:active {
  transform: translateY(-1px);
}

.login-footer {
  text-align: center;
  padding-top: 25px;
  border-top: 1px solid #f0f0f0;
}

.register-text {
  font-size: 15px;
  color: #5a6c7d;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.register-link {
  color: #ff6b35;
  text-decoration: none;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.3s ease;
  padding: 4px 8px;
  border-radius: 8px;
}

.register-link:hover {
  background: rgba(255, 107, 53, 0.1);
  transform: translateX(4px);
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes bounceIn {
  0% {
    opacity: 0;
    transform: scale(0.3);
  }
  50% {
    opacity: 1;
    transform: scale(1.05);
  }
  70% {
    transform: scale(0.9);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 15px;
  }
  
  .login-card {
    padding: 40px 30px;
    border-radius: 20px;
  }
  
  .logo-icon {
    width: 70px;
    height: 70px;
  }
  
  .login-title {
    font-size: 28px;
  }
  
  .login-subtitle {
    font-size: 15px;
  }
  
  .custom-input :deep(.el-input__wrapper) {
    padding: 10px 14px;
  }
  
  .custom-input :deep(.el-input__inner) {
    font-size: 15px;
  }
  
  .login-btn {
    height: 48px;
    font-size: 15px;
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    padding: 35px 25px;
    border-radius: 16px;
  }
  
  .logo-icon {
    width: 60px;
    height: 60px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .login-subtitle {
    font-size: 14px;
  }
  
  .form-item {
    margin-bottom: 20px;
  }
  
  .custom-input :deep(.el-input__wrapper) {
    padding: 8px 12px;
  }
  
  .custom-input :deep(.el-input__inner) {
    font-size: 14px;
  }
  
  .login-btn {
    height: 45px;
    font-size: 14px;
  }
  
  .register-text {
    font-size: 14px;
    flex-direction: column;
    gap: 4px;
  }
}
</style> 